package com.maoye.ossp.biz.intent.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hotent.base.api.query.QueryFilter;
import com.hotent.base.api.query.QueryOP;
import com.hotent.base.core.util.string.StringUtil;
import com.maoye.ossp.biz.contract.entity.AssociatedContract;
import com.maoye.ossp.biz.contract.entity.Assshops;
import com.maoye.ossp.biz.contract.entity.SelfAssociated;
import com.maoye.ossp.biz.contract.entity.SelfAssshops;
import com.maoye.ossp.biz.contract.service.IContractService;
import com.maoye.ossp.biz.contract.service.ISelfAssociatedService;
import com.maoye.ossp.biz.contract.utils.ContractUtils;
import com.maoye.ossp.biz.intent.entity.IntAssdiscard;
import com.maoye.ossp.biz.intent.entity.IntSelfdiscard;
import com.maoye.ossp.biz.intent.service.IIntAssdiscardService;
import com.maoye.ossp.biz.intent.service.IIntSelfdiscardService;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.org.util.UserUtils;

/**
 * 对象功能:自营作废意向 控制器类 
 * 开发公司:MOI 
 * 开发人员:zhongjt 
 * 创建时间:2014-02-20 14:50:38
 */

@Controller
@RequestMapping("biz/intent/intSelfdiscard")
public class IntSelfdiscardController extends BaseController {
	@Resource
	private IIntSelfdiscardService intSelfdiscardService;
	@Resource
	private ISelfAssociatedService  contractServce;
	/**
	 * 列表展示意向信息
	 * @param intSelfdiscard
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @author yangh
	 * @data 2014年4月11日
	 */
	@RequestMapping(value = "list")
	public String list(IntSelfdiscard intSelfdiscard, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		// TODO 等待宏天分页功能完善   Page page = new Page();
		QueryFilter queryFilter = getQuerFilter(request,intSelfdiscard);
		String createBy = null;
		if(StringUtil.isNotEmpty(intSelfdiscard.getCreateBy())){
			createBy = ContractUtils.getUserId(intSelfdiscard.getCreateBy());
		}
		queryFilter.addFilter("createBy", createBy, QueryOP.EQUAL);
		queryFilter.addFilter("area", intSelfdiscard.getArea(), QueryOP.EQUAL);
		queryFilter.addFilter("areaId", intSelfdiscard.getAreaId(), QueryOP.EQUAL);
		queryFilter.addFilter("floor", intSelfdiscard.getFloor(), QueryOP.EQUAL);
		queryFilter.addFilter("floorId", intSelfdiscard.getFloorId(), QueryOP.EQUAL);
		queryFilter.addFilter("intentNo", intSelfdiscard.getIntentNo(), QueryOP.EQUAL);
		queryFilter.addFilter("store", intSelfdiscard.getStore(), QueryOP.EQUAL);
		queryFilter.addFilter("storeId", intSelfdiscard.getStoreId(), QueryOP.EQUAL);
		queryFilter.addFilter("supplierNo", intSelfdiscard.getSupplierNo(), QueryOP.EQUAL);
		queryFilter.addFilter("status", intSelfdiscard.getStatus(), QueryOP.EQUAL);
		queryFilter.addFilter("createTime", intSelfdiscard.getCreateTime(), QueryOP.EQUAL);
		Page<IntSelfdiscard> page = intSelfdiscardService.find(queryFilter);
		model.addAttribute("page", page);
		return resultPage("/biz/intent/intSelfdiscard/list", model, request);
	}

	@RequestMapping(value = "save")
	public String save(IntSelfdiscard intSelfdiscard, Model model,
			RedirectAttributes redirectAttributes, HttpServletRequest request,
			HttpServletResponse response) {
		String navId = request.getParameter("navId");
		intSelfdiscardService.save(intSelfdiscard);
		addMessage(redirectAttributes, "保存'" + intSelfdiscard.getSupplierName()
				+ "合同作废意向'成功");
		return "redirect:/biz/intent/intSelfdiscard/list?navId="+navId;
	}
	
	@RequestMapping(value = "submit")
	public String submit(IntSelfdiscard intSelfdiscard, Model model,
			RedirectAttributes redirectAttributes, HttpServletRequest request,
			HttpServletResponse response) {
		String navId = request.getParameter("navId");
		intSelfdiscardService.submit(intSelfdiscard);
		addMessage(redirectAttributes, "提交'" + intSelfdiscard.getSupplierName()
				+ "合同作废意向'成功");
		return "redirect:/biz/intent/intSelfdiscard/list?navId="+navId;
	}
	
	
	@RequestMapping(value = "{contractId}/add")
	public String add( @PathVariable Long contractId,Model model, HttpServletRequest request) {
		String navId =request.getParameter("navId");
		model = intSelfdiscardService.init(contractId,model);
		model.addAttribute("navId", navId);
		return "/biz/intent/intSelfdiscard/add";
	}

	@RequestMapping(value = "{id}/edit")
	public String edit(@PathVariable Long id, HttpServletRequest request, Model model) {
		IntSelfdiscard intSelfdiscard = intSelfdiscardService.get(id);
		SelfAssociated  associatedContract  = contractServce.get(intSelfdiscard.getContractId().toString());
		model.addAttribute("intSelfdiscard", intSelfdiscard);
		model.addAttribute("associatedContract", associatedContract);
		return resultPage("/biz/intent/intSelfdiscard/add", model, request);
	}

	@RequestMapping(value = "{id}/view")
	public String view(@PathVariable Long id,HttpServletRequest request, Model model) {
		IntSelfdiscard intSelfdiscard = intSelfdiscardService.get(id);
		SelfAssociated  associatedContract  = contractServce.get(intSelfdiscard.getContractId().toString());
		model.addAttribute("intAssdiscard", intSelfdiscard);
		model.addAttribute("associatedContract", associatedContract);
		return resultPage("/biz/intent/intSelfdiscard/view", model, request);
	}

	@RequestMapping(value = "{id}/delete")
	public String delete(@PathVariable Long id, Model model,
			RedirectAttributes redirectAttributes) {
		intSelfdiscardService.remove(id);
		addMessage(redirectAttributes, "删除合同作废意向成功！");
		return "redirect:/biz/intent/intSelfdiscard/list";
	}
	/**
	 * 修改合同状态
	 * @param model
	 * @param redirectAttributes
	 * @return
	 * @author yangh
	 * @data 2014年4月10日
	 */
	@RequestMapping(value = "updateContractType")
	public String updateContractType( Model model,
			RedirectAttributes redirectAttributes) {
		return "redirect:/biz/intent/intSelfdiscard/list";
	}

	/**
	 * 批量刪除
	 * @param ids
	 * @param request
	 * @param model
	 * @param redirectAttributes
	 * @return
	 * @author yangh
	 * @data 2014年5月21日
	 */
	@RequestMapping(value = "{ids}/removeBatch")
	public String removeBatch(@PathVariable String ids, HttpServletRequest request,Model model,RedirectAttributes redirectAttributes) {
		String item = intSelfdiscardService.removeBatch(ids);
		if(!"".equals(item)){
			addMessage(redirectAttributes,"意向单号为"+item+"的作废意向已经进入流程审批，不能被删除！");
		}else{
			addMessage(redirectAttributes,"删除作废意向成功");
		}
		String navId = request.getParameter("navId");
		return "redirect:/biz/intent/intSelfdiscard/list?navId="+navId;
	}
	
	/**
	 * 获取合同信息
	 * @param id
	 * @param model
	 * @param redirectAttributes
	 * @return
	 * @author yanghao
	 * @data 2014年3月28日
	 */
	@RequestMapping(value = "getModuleContractList")
	public String getModuleContractList(AssociatedContract associatedContract ,HttpServletRequest request,HttpServletResponse response,Model model) {
		QueryFilter queryFilter = getQuerFilter(request,associatedContract);
		Page<SelfAssociated> page = contractServce.find(queryFilter);
		int totalSize = (page.getTotal() + page.getPageSize() - 1) / page.getPageSize();
		model.addAttribute("page", page);
		model.addAttribute("totalSize", totalSize);
		return resultPage("/biz/intent/common/modal", model, request);
	}
	/**
	 * 获取合同信息(分页)
	 * @param id
	 * @param model
	 * @param redirectAttributes
	 * @return
	 * @author yanghao
	 * @throws IOException 
	 * @data 2014年3月28日
	 */
	@RequestMapping(value = "getModuleContractListPage")
	public void getModuleContractListPage(AssociatedContract associatedContract ,HttpServletRequest request,HttpServletResponse response,Model model) throws IOException {
		
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/plain");
		PrintWriter out = response.getWriter();
		QueryFilter queryFilter = getQuerFilter(request,associatedContract);
		String supplierName =associatedContract.getSupplierName();
		String contractNo = associatedContract.getContractNo();
		String subject = associatedContract.getSubject();
		queryFilter.addFilter("supplierName", supplierName!=""?supplierName:null, QueryOP.LIKE);
		queryFilter.addFilter("contractNo", contractNo !=""?contractNo:null, QueryOP.LIKE);
		queryFilter.addFilter("subject", subject!=""?subject:null, QueryOP.LIKE);
		Page<SelfAssociated> page = (Page<SelfAssociated>) contractServce.find(queryFilter);
		List<SelfAssociated> list = page.getList();
		
		StringBuffer sb = new StringBuffer(); 
		sb.append("{\"total\":\""+page.getTotal()+"\",");
		sb.append("\"pageNo\":\""+page.getPageNo()+"\",");
		sb.append("\"pageSize\":\""+page.getPageSize()+"\",");
		sb.append("\"jsonRoot\":[");
		
		for(int i =0;i<list.size();i++){
			SelfAssociated  contract = list.get(i);
			sb.append("{\"id\":\""+ contract.getId());  
            sb.append("\",");
            sb.append("\"supplierName\":\""+contract.getSupplierName()+"\",");
            sb.append("\"subject\":\""+contract.getSubject()+"\",");
            sb.append("\"contractNo\":\""+contract.getContractNo()+"\"");
			sb.append("},");
		}
		if(list.size()>0){
			sb.deleteCharAt(sb.lastIndexOf(","));
		}
		sb.append("]}"); 
        out.print(sb);
        out.flush();
		out.close();
	}
}
